package com.chuji.crmcj.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuji.crmcj.R;
import com.chuji.crmcj.act.ClientDetailAct;
import com.chuji.crmcj.act.LendHistoryActivity;
import com.chuji.crmcj.bean.AgreeOrderInfo;
import com.chuji.crmcj.bean.LoanRecordHistoryInfo;
import com.chuji.crmcj.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by y.yiwen on 2/19/2016.
 */
public class CustomClientLendInfoAdapter extends BaseExpandableListAdapter {
    //数据存储
    List<LoanRecordHistoryInfo.ApiParamObjEntity> loanRecordHistoryData;
    //网络数据
    private LoanRecordHistoryInfo loanRecordHistoryInfo;
    /*历史记录*/
    private ExpandableListView mLv_lend_history;
    private ImageView mIv_client_tag_no;
    private TextView mTv_client_name;
    private TextView mTv_client_phoneNumber;
    private TextView mTv_use_number;
    private LinearLayout ll_next1;
    private LinearLayout ll_next2;
    private ImageView  iv_arr;
    public CustomClientLendInfoAdapter(LendHistoryActivity lendHistoryActivity, List<LoanRecordHistoryInfo.ApiParamObjEntity> loanRecordHistoryData, ExpandableListView mLv_lend_history) {
        this.loanRecordHistoryData = loanRecordHistoryData;
        this.mLv_lend_history = mLv_lend_history;
    }
    @Override
    public int getGroupCount() {
        return loanRecordHistoryData.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return i;
    }

    @Override
    public Object getGroup(int i) {
        return loanRecordHistoryData.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return null;
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int i, boolean isExpanded, View convertView, ViewGroup viewGroup) {
        //b)声明ViewHolder
//        ViewHolder viewHolder = null;
        //c)判断convertView是否为null
//        if (convertView == null) {//第一次调用getView（）方法时为 null,意思就是在内存中没有
//            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(UIUtils.getContext()).inflate(R.layout.listview_lend_history_group, null);
            //初始化组件
            mIv_client_tag_no = (ImageView) convertView.findViewById(R.id.iv_client_tag_no);
            mTv_client_name = (TextView) convertView.findViewById(R.id.tv_name);
            mTv_client_phoneNumber = (TextView) convertView.findViewById(R.id.tv_number);
            mTv_use_number = (TextView) convertView.findViewById(R.id.tv_num);
            ll_next1 = (LinearLayout) convertView.findViewById(R.id.ll_next1);
            ll_next2 = (LinearLayout) convertView.findViewById(R.id.ll_next2);
            iv_arr = (ImageView) convertView.findViewById(R.id.iv_arr);
            if (!isExpanded) {
                iv_arr.setImageDrawable(UIUtils.getDrawable(R.drawable.icondown));
            } else {
                iv_arr.setImageDrawable(UIUtils.getDrawable(R.drawable.iconup));
            }
//            convertView.setTag(viewHolder);
//        } else {
////            viewHolder = (ViewHolder) convertView.getTag();
//        }
        //跳转到详情界面
        ll_next1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UIUtils.startActivity(new Intent(UIUtils.getContext(), ClientDetailAct.class));
            }
        });
        ll_next2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UIUtils.startActivity(new Intent(UIUtils.getContext(), ClientDetailAct.class));
            }
        });
        return convertView;
    }
//    class ViewHolder{
//        private ImageView mIv_client_tag_no;
//        private TextView mTv_client_name;
//        private TextView mTv_client_phoneNumber;
//        private TextView mTv_use_number;
//        private LinearLayout ll_next1;
//        private LinearLayout ll_next2;
//    }
    @Override
    public void onGroupExpanded(int groupPosition) {
        super.onGroupExpanded(groupPosition);
        int totalGroup = getGroupCount();
        for (int i = 0; i < totalGroup; i++) {
            if (i != groupPosition) {
                mLv_lend_history.collapseGroup(i);
                iv_arr.setImageDrawable(UIUtils.getDrawable(R.drawable.icondown));
                this.notifyDataSetChanged();
            }
        }
    }
    @Override
    public View getChildView(int i, int i1, boolean b, View convertView, ViewGroup viewGroup) {
        //b)声明ViewHolder
        ViewHolder2 viewHolder = null;
        //c)判断convertView是否为null
        if (convertView == null) {//第一次调用getView（）方法时为 null,意思就是在内存中没有
            viewHolder = new ViewHolder2();
            convertView = LayoutInflater.from(UIUtils.getContext()).inflate(R.layout.listview_lend_history_child, null);
            //初始化组件
            viewHolder.mTv_lend_time = (TextView) convertView.findViewById(R.id.tv_loan_date);
            viewHolder.mTv_lend_money = (TextView) convertView.findViewById(R.id.tv_loan_money);
            viewHolder.mTv_total_money = (TextView) convertView.findViewById(R.id.tv_totle_cost);
            viewHolder.mTv_repay_month = (TextView) convertView.findViewById(R.id.tv_repay_month);
            viewHolder.mTv_extra_expense = (TextView) convertView.findViewById(R.id.tv_additional_cost);
            viewHolder.mTv_lend_rate = (TextView) convertView.findViewById(R.id.tv_loan_lixi);
            viewHolder.mTv_lend_years = (TextView) convertView.findViewById(R.id.tv_loan_year_limt);
            convertView.setTag(viewHolder);
        } else {
            viewHolder= (ViewHolder2) convertView.getTag();
        }
        return convertView;
    }

    static class ViewHolder2 {
        private TextView mTv_lend_time;
        private TextView mTv_lend_money;
        private TextView mTv_total_money;
        private TextView mTv_repay_month;
        private TextView mTv_extra_expense;
        private TextView mTv_lend_rate;
        private TextView mTv_lend_years;
    }
    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
}
